package com.reading.message.service.impl;

import com.reading.common.cache.constant.SmsCodeType;
import com.reading.common.cache.service.CacheService;
import com.reading.common.utils.StringUtil;
import com.reading.common.utils.WebUtil;
import com.reading.message.config.SmsConfig;
import com.reading.message.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author wangchaodz@gmail.com
 * @since  2018/9/18 14:08
 */
@Slf4j
@Service
public class SmsServiceImpl implements SmsService {

    @Resource
    private SmsConfig smsConfig;

    @Resource
    private CacheService cacheService;

    @Override
    public int sendSmsCode(String smsCodeType, String mobileNo, String smsCode) {
        try {
            SmsCodeType smsType = SmsCodeType.getByTypeCode(smsCodeType);
            boolean isSendOK = doSendSmsCode(mobileNo, smsCode);
            if (isSendOK) {
                //发送成功后，存储短信验证码缓存
                cacheService.setSmsCode(smsType, mobileNo, smsCode);
                return 0;
            }
        } catch (Exception e) {
            log.error("向{}发送短信验证码失败,", mobileNo, e);
        } finally {
            if (log.isWarnEnabled()) {
                log.warn("向{}发送短信验证码类型：{}", mobileNo, smsCodeType);
            }
        }
        return -1;
    }

    private boolean doSendSmsCode(String mobileNo, String smsCode) {
        String respStr = null;
        try {
            if (!smsConfig.isSendSmsFlag()) {
                if (log.isWarnEnabled()) {
                    log.warn("配置文件不发送短信验证码,mobile:{},smsCode:{}", mobileNo, smsCode);
                }
                return true;
            }
            StringBuilder requestUrl = new StringBuilder(smsConfig.getSmsUrl());
            requestUrl.append("?name=").append(smsConfig.getSmsName());
            requestUrl.append("&pwd=").append(smsConfig.getSmsPwd());
            requestUrl.append("&content=").append("短信验证码:").append(smsCode);
            requestUrl.append("&mobile=").append(mobileNo);
            requestUrl.append("&stime=");
            requestUrl.append("&sign=").append(smsConfig.getSmsSign());
            requestUrl.append("&type=").append(smsConfig.getSmsType());
            requestUrl.append("&extno=");
            respStr = WebUtil.httpRequest(requestUrl.toString(), null, "GET", null);
            //0,2018081720173842530561620,0,1,0,提交成功
            if (StringUtil.isEmpty(respStr) || !respStr.startsWith("0,")) {
                if (log.isErrorEnabled()) {
                    log.error("向{}发送短信验证码:{},失败,{}", mobileNo, smsCode, respStr);
                }
                return false;
            }
            return true;
        } catch (Exception e) {
            log.error("向{}发送短信验证码:{}异常,{}", mobileNo, smsCode, respStr);
            return false;
        } finally {
            if (log.isWarnEnabled()) {
                log.warn("向{}发送短信验证码:{}", mobileNo, smsCode);
            }
        }
    }

    @Override
    public boolean checkSmsCode(String smsCodeType, String mobileNo, String smsCode) {
        try {
            if (StringUtil.isEmpty(mobileNo) || StringUtil.isEmpty(smsCode)) {
                if (log.isWarnEnabled()) {
                    log.warn("短信验证码验证，mobileNo:{},smsCode:{}", mobileNo, smsCode);
                }
                return false;
            }
            SmsCodeType smsType = SmsCodeType.getByTypeCode(smsCodeType);
            return cacheService.checkSmsCode(smsType, mobileNo, smsCode);
        } catch (Exception e) {
            log.error("短信验证码验证异常，mobileNo:{},smsCode:{}", mobileNo, smsCode);
            return false;
        }
    }
}
